package com.cbank.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.cbank.domain.Company;

@Repository
public interface CompanyDao {

	public final static String ALL = "id,user_name,phone,name,company_id,registe_price,registe_time,if_off,city,industry,status,resource,count,update_time,create_time";
	public final static String CALL = "c.id,c.user_name,c.phone,c.name,c.company_id,c.registe_price,c.registe_time,c.if_off,c.city,c.industry,c.status,c.resource,c.count,c.update_time,c.create_time";

	@Insert("insert into company(user_name,phone,name,company_id,registe_price,registe_time,if_off,city,industry,status,resource,count)"
			+ " values(#{userName},#{phone},#{name},#{companyId},#{registePrice},#{registeTime},#{ifOff},#{city},#{industry},#{status},#{resource},#{count})")
	@Options(useGeneratedKeys = true)
    void insert(Company obj);

	@Select("select " + ALL + " from company where id = #{id} limit 1")
	public Company getById(Long id);


	@Select("select " + CALL
			+ " from company c left join (select uc.company_table_id id from user_company uc where uc.user_id=#{userId}) u on c.id = u.id where u.id is null limit #{offset}")
	public List<Company> getPage(@Param(value = "userId") long userId,@Param(value = "index") int index, @Param(value = "offset") int offset);
	@Select("select " + CALL
			+ " from company c left join (select uc.company_table_id id from user_company uc where uc.user_id=#{userId}) u on c.id = u.id where u.id is null and c.city = #{city} and c.industry = #{industry} limit #{offset}")
	public List<Company> getPageWithParam(@Param(value = "userId") long userId,@Param(value = "index") int index, @Param(value = "offset") int offset,
			@Param(value = "city") long city, @Param(value = "industry") long industry);

	@Select("select " + CALL
			+ " from company c left join (select uc.company_table_id id from user_company uc where uc.user_id=#{userId}) u on c.id = u.id where u.id is null and c.city = #{city} limit #{offset}")
	public List<Company> getPageWithCity(@Param(value = "userId") long userId,@Param(value = "index") int index, @Param(value = "offset") int offset,
			@Param(value = "city") long city);

	@Select("select " + CALL
			+ " from company c left join (select uc.id,uc.view_id,uc.user_id,uc.phone_num,uc.status,uc.create_time,uc.update_time from user_company uc where uc.user_id=#{userId}) u on c.id = u.id where u.id is null and c.industry = #{industry} limit #{offset}")
	public List<Company> getPageWithIndustry(@Param(value = "userId") long userId,@Param(value = "index") int index, @Param(value = "offset") int offset,
			@Param(value = "industry") long industry);

	@Select("select count(1) from company where status = 1")
    int getCount();
}
